import csv
import urllib.request

def indep_compile_gate_support(args):
    project_indep_compile_support = {}
    url = "https://raw.gitcode.com/openharmony/manifest/raw/master/matrix_product.csv"
    response = urllib.request.urlopen(url, timeout=10)
    data = response.read().decode("utf-8")

    local_file = "matrix_product.csv"
    with open(local_file, "w") as f:
        f.write(data)
    if args.build_type == "t":
        build_type = "part_compile_tdd"
    else:
        build_type = "part_compile"
    with open(local_file, mode='r', encoding='utf-8') as file:
        csv_reader = csv.DictReader(file)
        for row  in csv_reader:
            support = row.get(build_type,"")
            repoistory = row.get("repoistory", "")
            if support == "" or support == "N":
                continue
            else:
                project_indep_compile_support[repoistory] = True
    return project_indep_compile_support


def get_project_indep_compile_support_status(project_dict, args):
    support_dict = indep_compile_gate_support(args)
    support_result = {}
    all_project_support_indep_compile = True
    for project in project_dict:
        if project not in support_dict:
            print(f"Warning: {project} is not in the matrix_product.csv")
            print(f"Please check the matrix_product.csv")
            support_result[project] = False
            all_project_support_indep_compile = False
        else:
            support_result[project] = True
    return all_project_support_indep_compile, support_result